﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Products_ctrProductList_Homepage : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindData();
        }
    }

    private void BindData()
    {
        lblBestProduct.Text = MultipleLanguage.LBL_HIGHTLIGHT_PRODUCT[nCurrentLang - 1].ToUpper() ;  
       
        int nTrademark = 0;
        int nMaterial = 0;
        double dPriceFrom = 0;
        double dPriceTo = 100000000;
        string sProductName = "";
        int nStatus = 1;
        int nPageCount = 0;
        int nItemView = 12;
        int nItemID = 0;
        int isSaleOff = -1;
        int nPageIndex = 1;
        int nPageSize = 12;
        Products objProduct = new Products();
        DataTable dtbProduct = new DataTable();
        dtbProduct = objProduct.Search(nItemView, "", sProductName, nItemID,string.Empty , nTrademark, nMaterial, dPriceFrom, dPriceTo, isSaleOff, -1, nStatus, 0, 0, 0, nDefaultLanguage, nCurrentLang, nPageIndex, nPageSize, ref nPageCount);
        rptProduct.DataSource = dtbProduct;
        rptProduct.DataBind();


    }
    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string sCode = string.Empty;
        string sContact = string.Empty;
        string sPrice = string.Empty;
        int nCurrentLang = Support.GetCurrentLanguage();

        sPrice = MultipleLanguage.PRICE[nCurrentLang - 1] + ": ";
        sContact = MultipleLanguage.PRICE_CALL[nCurrentLang - 1];
        sCode = MultipleLanguage.PROCODE[nCurrentLang - 1];

        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnProductImage = (ImageButton)e.Item.FindControl("btnProductImage");
            HyperLink lnkProductName = (HyperLink)e.Item.FindControl("lnkProductName");
            Label lblCode = (Label)e.Item.FindControl("lblCode");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            LinkButton lnkAddCart = (LinkButton)e.Item.FindControl("lnkAddCart");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            string sProductName = ConvertData.ConvertToString (DataBinder.Eval(e.Item.DataItem, "ProductName"));
            if (sProductName.Length >0)
            {
                lnkProductName.Text = sProductName;
            }
            else
            {
                lnkProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName_Default"));
            }
           
            lblCode.Text = sCode + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            string sToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent"));
            if (sToolTip.Length > 0)
            {
                btnProductImage.ToolTip = sToolTip;
            }
            else
            { btnProductImage.ToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent_Default")); }
            double dPrice = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
            if (dPrice > 0)
            { lblPrice.Text = sPrice +  ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price"))) +" " + "₫"; }
            else { lblPrice.Text = sPrice + sContact; }
         
            lnkProductName.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID + "=" + nID;
            lnkAddCart.Text = MultipleLanguage.BUY[nCurrentLang - 1];  
            //lnkAddCart.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ADD_BASKET_SUCCCESS[nCurrentLang-1]);
            btnProductImage.ImageUrl = Config.ROOT + "Images/Upload/Products/" + ConvertData.ConvertToString (DataBinder.Eval(e.Item.DataItem, "ImageDefault"));
            btnProductImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lnkAddCart.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
        }
    }
    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?" + Config.PRO_ID+ "=" + nID;
                Response.Redirect(sDetail);
                break;
            case "addcart":
                // Add product to Cart
                clsBasket objBasket = clsBasket.getclsBaskets (Context);
                ItemclsBaskets  item = new ItemclsBaskets (nID);
                objBasket.AddItem(item);
                int nCat = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT]);
                if (nCat > 0)
                { Response.Redirect(this.GetCurrentPageName() + "?" + Config.PRO_CAT + "=" + nCat); }
                else
                {
                    Response.Redirect(this.GetCurrentPageName());
                }
                break;
        }
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
    private string GetTrademark(int nID)
    {
        ProductTradeMark obj = new ProductTradeMark();
        obj.LoadById(nID);
        string sReturn = obj.Data.TradeMarkName;
        return sReturn;
    }
}
